/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.uoc.tfc.exuoc.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.OneToMany;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Version;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.Cascade;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import edu.uoc.tfc.exuoc.config.Config;

/**
 *
 * @author emili
 */
@Entity
@Table(name="grups")
public class Grup implements Serializable , Comparable {
    private static final long serialVersionUID = 3245235352353L;

    private Long id=Config.valorNull;        //character varying(15) NOT NULL,
    private String nom=Config.valorNullStr;     //NOT NULL DEFAULT ('now'::text)::date,
    private Date dataPub=new Date();
    private int versionNum=0;

    // E-R amb Uusaris
    private List<Usuari> usuaris=new ArrayList<Usuari>(0);



    public Grup(){ }

    public Grup(String nom) {
        this.nom = nom;
    }

    public Grup(String nom, Date dataPub) {
        this.nom = nom;
        this.dataPub = dataPub;
    }

    public Grup(Long _id, String _nom, Date _dataPub) {
        this.emplenar(_id, _nom, _dataPub);
    }

    private void emplenar(Long _id,String _nom,Date _datapub){

        if (Long.valueOf(_id).equals(null)){  this.id=Config.valorNull;}
        else this.id=_id;

        if (_nom.equals(null)) this.nom=Config.valorNullStr;
        else this.nom=_nom;

        if (_datapub.equals(null)) this.dataPub = new java.util.Date();
        else this.dataPub=_datapub;

    }

    @Id
    @SequenceGenerator(name="grups", sequenceName="seqgrups")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="grups")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name="datapub", nullable=false)
    public Date getDatapub() {
        return dataPub;
    }

    public void setDatapub(Date dataPub) {
        this.dataPub = dataPub;
    }

    @Column(length=15,nullable=false,unique=true)
    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

   // Control d'exceptions :StaleObjectStateException
    @Version
    @Column(name="version", nullable=true)
    protected int getVersionNum() {
        return versionNum;
    }

    protected void setVersionNum(int versionNum) {
        this.versionNum = versionNum;
    }

    @OneToMany(cascade=javax.persistence.CascadeType.ALL, mappedBy="grup")
    //Emili:11-01-2012:, fetch=javax.persistence.FetchType.EAGER

    @OnDelete(action = OnDeleteAction.CASCADE)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)

    public List<Usuari> getUsuaris() {
        return usuaris;
    }

    public void setUsuaris(List<Usuari> _Usuaris) {
        this.usuaris = _Usuaris;
    }

    public void addUsuari(Usuari _usuari)
    {
        //Ja el tenim ?
        if ( ! this.usuaris.contains(_usuari))
            this.usuaris.add((Usuari)_usuari);
    }

    public void delUsuari(Usuari _usuari)
    {
        //Existeis l'usuari ?
        if (this.usuaris.contains(_usuari))
            this.usuaris.remove(_usuari);
    }


    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Grup other = (Grup) obj;
        //if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {

        if (this.id !=other.id){
            return false;
        }
        if ((this.nom == null) ? (other.nom != null) : !this.nom.equals(other.nom)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0 );
        hash = 97 * hash + (this.nom != null ? this.nom.hashCode() : 0);
        hash = 97 * hash + (this.dataPub != null ? this.dataPub.hashCode() : 0);
        return hash;
    }

    
    @Override
    public String toString() {
        return id + ";"+nom+";"+dataPub.toString();
    }

    @Override
    public int compareTo(Object o) {
        Grup i =null;
        if (o == null) {
            return -2;
        }
        if (!this.getClass().equals( o.getClass())) {
           return -2;
        }
        i=(Grup)o;

        return (this.equals(i))? 0:1;
    }
public static void main (String[] args){

    Grup gp1;
    Grup gp2;

    gp1=new Grup();
    gp1.setId(1L);
    gp1.setNom("grup1");

    gp2=new Grup();
    System.out.println("- "+gp1.compareTo(gp2));
    gp2.setId(1L);
    gp2.setNom("grup1");
    System.out.println("- "+gp1.compareTo(gp2));
}

}
